package com.cysion.study.ui.fragment

import android.support.v7.widget.LinearLayoutManager
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.adapter.CourseMoreAdapter
import com.cysion.study.entity.CourseEntity
import com.cysion.study.presenter.TeacherPresenter
import com.cysion.study.ui.iview.TeacherView
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.fragment_course_more.*

//课表
class CourseWeekFragment : BaseFragment(), TeacherView {

     var type: String = ""

    //绑定presenter
    private val presenter by lazy {
        TeacherPresenter().apply {
            attach(this@CourseWeekFragment)
        }
    }
    override fun getLayoutId(): Int = com.cysion.study.R.layout.fragment_course_more


    val simpleDatas = arrayListOf<CourseEntity>()
    private lateinit var courseAdapter: CourseMoreAdapter
    override fun initView() {
        type = arguments?.getString("type").toString()

        initRecyclerView()
    }


    override fun lazyLoad() {
        super.lazyLoad()
        if (type == "1") {
            presenter.getCousel()
        }else if (type == "2") {
            presenter.getCouselMonth()
        }else if (type == "3") {
            presenter.getCouselAll()
        } else {
            presenter.getCousel()
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        recycler_view.isNestedScrollingEnabled = false
        recycler_view.layoutManager = LinearLayoutManager(context)
        courseAdapter = CourseMoreAdapter(simpleDatas, Box.context)
        recycler_view.adapter = courseAdapter
        courseAdapter.notifyDataSetChanged()
    }


    override fun onGetCourse(carousels: MutableList<CourseEntity>) {
        simpleDatas.clear()
        if (carousels.size == 0) {
            multiView.showEmpty()
        } else {
            simpleDatas.addAll(carousels)
            courseAdapter.notifyDataSetChanged()
            multiView.showContent()
        }

    }

    override fun onGetCourseError(s: String) {
        multiView.showError()
    }

    override fun loading() {
    }

    override fun stopLoad() {
    }

    override fun error(code: Int, msg: String) {
        ShowToast(msg)

    }


    override fun closeMvp() {
        presenter.detach()
    }

}
